<?php
namespace app\api\controller;

use app\api\controller\Base;
use think\Db;



class Index extends Base
{
    public function _empty()
    {
        header("Location:/404.html");
        exit;
    }
    public function ajax()
    {
        $data=input('post.');
		if(!$lizhili=Base::lizhili($data)){
			header("Location:/404.html");
			exit;
		}
        if($lizhili==='lizhili1'){
            return ['code'=>10001,'message'=>'提交超时，请重复提交！'];
        }
        if($lizhili==='lizhili'){
            return ['code'=>10002,'message'=>'重复提交，请稍后重试！'];
        }
		
		
		//获取相关
		if ($data['type']=='get_xiangguan') {
			$res='';
			if($data['lei']=='yinsi'){
				$res=Db::name('shuoming')->find(1);
			}
			if($data['lei']=='fuwu'){
				$res=Db::name('shuoming')->find(2);
			}
			if($res){
				return ['code'=>1,'message'=>'成功','data'=>$res];
			}else{
				return ['code'=>0,'message'=>'失败'];
			}
		}
		
		
		//app  升级
		if ($data['type']=='update_app') {
		    return ['code'=>1,'app_v'=>config('app_v'),'url'=>'http://'.\request()->host().'/update/update_'.config('app_v').'.apk','desc'=>config('app_desc')];
		}
		 //查看是否有权限
		if ($data['type']=='get_my_check') {
			if($user=Db::name('user')->where('token',$data['token'])->find()){
				return ['code'=>1,'message'=>'成功'];
			}else{
				return ['code'=>0,'message'=>'失败'];
			}
		}
		//微信登陆
		if ($data['type']=='get_login_wx_user') {
			$raw=\json_decode($data['rawData'],true);
			$appid=config('wx_config')['appid'];
			$secret=config('wx_config')['secret'];
			$code=$this->curl('https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$secret.'&js_code='.$data['code'].'&grant_type=authorization_code');
			if($code and $raw){
				$raw['openid']=$code['openid'];
				if($user=Db::name('user')->where('openid_wx',$code['openid'])->find()){
					$token=md5($user['openid_wx'].time());
					Db::name('user')->where('id',$user['id'])->update(['token'=>$token,'openid_wx'=>$code['openid']]);
					if(!$user['fid'] and $data['fid']){
						Db::name('user')->where('id',$user['id'])->update(['fid'=>$data['fid']]);
					}
					return ['code'=>1,'message'=>'已经绑定微信','token'=>$token,'uid'=>$user['id']];
				}else{
					$raw['token']=md5($raw['openid'].time());
					$linshi=\model('user');
					$raw['openid_wx']=$raw['openid'];
					$raw['unionid']=$code['unionid'] ?: '';
					$linshi->save($raw);
					if($data['fid']){
						Db::name('user')->where('id',$linshi->id)->update(['fid'=>$data['fid']]);
					}
					Db::name('user')->where('id',$linshi->id)->update(['user_no'=>100000+$linshi->id]);
					return ['code'=>1,'message'=>'已经绑定微信','token'=>$raw['token'],'uid'=>$linshi->id];
				}
			}else{
				return ['code'=>0,'message'=>'登陆失败'];
			}
		}
		//微信登陆 app登陆
		if ($data['type']=='get_login_wx_app') {
		    $info=\json_decode($data['info'],true);
		    if($info['unionId']){
		        if($user=Db::name('user')->where('unionId',$info['unionId'])->find()){
		            $token=md5($user['openid_app'].time());
		            Db::name('user')->where('id',$user['id'])->update(['token'=>$token,'openid_app'=>$info['openId']]);
		            if(!$user['fid'] and $data['fid']){
		                Db::name('user')->where('id',$user['id'])->update(['fid'=>$data['fid']]);
		            }
		            return ['code'=>1,'message'=>'已经绑定微信','token'=>$token,'uid'=>$user['id']];
		        }else{
		            $raw=[];
		            $raw['token']=md5($info['openId'].time());
		            $linshi=\model('user');
		            $raw['openid_app']=$info['openId'];
		            $raw['nickName']=$info['nickName'];
		            $raw['gender']=$info['gender'];
		            $raw['city']=$info['city'];
		            $raw['province']=$info['province'];
		            $raw['country']=$info['country'];
		            $raw['avatarUrl']=$info['avatarUrl'];
		            $raw['unionId']=$info['unionId'];
		            $linshi->save($raw);
		            if($data['fid']){
		                Db::name('user')->where('id',$linshi->id)->update(['fid'=>$data['fid']]);
		            }
		            Db::name('user')->where('id',$linshi->id)->update(['user_no'=>100000+$linshi->id]);
		
		            return ['code'=>1,'message'=>'已经绑定微信','token'=>$raw['token'],'uid'=>$linshi->id];
		        }
		    }else{
		        return ['code'=>0,'message'=>'登陆失败'];
		    }
		}
        return ['code'=>0,'message'=>'非法获取'];
    }
	
}
